package com.samson.launcherchange;

import android.content.ComponentName;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

/**
 * 动态更换应用Icon
 */
public class MainActivity extends AppCompatActivity {

    private ComponentName mLauncher;
    private ComponentName mLauncherA;
    private ComponentName mLauncherB;
    private PackageManager mPm;

    SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLauncher = getComponentName();
        mLauncherA = new ComponentName(getBaseContext(),"com.samson.launcherchange.Test2");
        mLauncherB = new ComponentName(getBaseContext(),"com.samson.launcherchange.Test3");

        mPm = getApplicationContext().getPackageManager();

        sp = getSharedPreferences("launcherChange", Context.MODE_PRIVATE);

        SharedPreferences.Editor editor = sp.edit();

        int a = sp.getInt("tag",0);

        switch (a){
            case 0:
                changeLauncherIcon1();
                editor.putInt("tag",1);
                editor.commit();
                break;
            case 1:
                changeLauncherIcon2();
                editor.putInt("tag",2);
                editor.commit();
                break;
            case 2:
                editor.putInt("tag",0);
                editor.commit();
                break;
        }
    }

    /**
     * 改变为Icon1
     * @param view
     */
    private void changeLauncherIcon1(){
        disableCompat(mLauncher);
        disableCompat(mLauncherB);
        enableCompat(mLauncherA);
    }

    /**
     * 改变为Icon2
     * @param view
     */
    private void changeLauncherIcon2(){
        disableCompat(mLauncher);
        disableCompat(mLauncherA);
        enableCompat(mLauncherB);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    /**
     *
     * @param componentName
     */
    private void enableCompat(ComponentName componentName){
        mPm.setComponentEnabledSetting(componentName,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    }

    /**
     *
     * @param componentName
     */
    private void disableCompat(ComponentName componentName){
        mPm.setComponentEnabledSetting(componentName,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }
}
